<?php

include_once '_database.php';

function logdb($model, $content, $type = 'INFO') {
  $time = date("Y-m-d H:i:s");
  $timestamp = (int)(microtime(true) * 1000);
  $type = strtoupper($type);
  $ctype = gettype($content);
  if ($ctype == 'object' || $ctype == 'array') {
    $content = json_encode($content, JSON_UNESCAPED_UNICODE);
  }
  $sql = "CREATE TABLE IF NOT EXISTS `2kt_logs` (`number` INT(16) NOT NULL AUTO_INCREMENT , `time` DATETIME on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `timestamp` bigint(128) NOT NULL , `model` VARCHAR(64) NULL DEFAULT NULL , `type` VARCHAR(8) NULL DEFAULT NULL , `content` TEXT NULL DEFAULT NULL , PRIMARY KEY (`number`)) ENGINE = InnoDB";
  exec_sql($sql);
  $sql = "INSERT INTO `2kt_logs`(`time`, `timestamp`, `model`, `type`, `content`) VALUES ('{$time}',{$timestamp},'{$model}','{$type}','{$content}')";
  $result = exec_sql($sql);
  return $result ? $result : $sql;
}
